package com.sdvdxl.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Calendar;

import org.apache.commons.lang3.StringUtils;

import com.sdvdxl.BlogNotFoundException;
import com.sdvdxl.model.BlogEntity;

/**
 * 摘要处理，正文保存到磁盘
 * 
 * @author sdvdxl
 *
 */
public class BlogHandler {
	private BlogEntity blog;
	private String summary;
	private boolean hasWritten;
	private static LoggerUtil logger = new LoggerUtil(GlobalVars.class);
	
	public BlogHandler(BlogEntity blogEntity) {
		this.blog = blogEntity;
	}
	
	private String getSummary() {
		//如果摘要为空，则截取正文的前150字为摘要，不足的有多少算多少
		if (StringUtils.isBlank(blog.getSummary())) {
			if (blog.getContent().length()>150) {
				summary = blog.getContent().substring(0,150);
			} else {
				summary = blog.getContent();
			}
		} else { //截取摘要的300字
			if (blog.getSummary().length()>300) {
				summary = blog.getSummary().substring(0,300);
			} else {
				summary = blog.getSummary();
			}
		}
		
		return summary;
	}
	
	public BlogEntity saveBlog() {
		if (!hasWritten) {
			blog.setSummary(getSummary());
			
			//保存到网站根目录下的blog目录，并且根据年，月在blog目录下创建年/ 月/目录
			writeBlogToDisk();
			hasWritten = true;
		}
		return blog;
	}
	
	private void writeBlogToDisk() {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(blog.getCreateTime());
		String path = cal.get(Calendar.YEAR) + "" + (cal.get(Calendar.MONTH)+1);
		String blogId = cal.get(Calendar.DAY_OF_MONTH)+ "_" 
				+ cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) 
				+ cal.get(Calendar.SECOND) + cal.get(Calendar.MILLISECOND) ;
		this.blog.setPath(path);
		this.blog.setBlogId(blogId);
		
		try {
			File f = new File(GlobalVars.getInstance().getBlogPath() + "/" + path);
			if (!f.exists()) {
				f.mkdirs();
			}
			f = new File(f.getAbsolutePath()+"/"+blogId);
			fos = new FileOutputStream(f); 
			osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			bw.write(blog.getContent());
			bw.flush();
		} catch (FileNotFoundException e) {
			logger.error(e);
		} catch (IOException e) {
			logger.error(e);
		} finally {
			try {
				if (null!=fos) {
					fos.close();
				}
				
				if (osw!=null) {
					osw.close();
				}
				
				if (bw!=null) {
					bw.close();
				}
			} catch (IOException e) {
				logger.error(e);
			}
		}
	}
	
	/**
	 * 返回拥有博文的实体
	 * @return 
	 * @throws IOException 
	 * @throws BlogNotFoundException 
	 */
	public BlogEntity readBlog() throws IOException {
		readBlogFromDisk();
		return this.blog;
	}
	
	private void readBlogFromDisk() throws IOException {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		
		try {
			StringBuilder sb = new StringBuilder();
			fis = new FileInputStream(GlobalVars.getInstance().getBlogPath()+"/" +blog.getPath()+"/"+blog.getBlogId());
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);
		
			String line = null;
			while ((line=br.readLine())!=null) {
				sb.append(line);
			}
			
			blog.setContent(sb.toString());
		}catch(FileNotFoundException e) {
			throw e;
		}catch (IOException e) {
			throw e;
		} finally {
			try {
				if (fis!=null) {
					fis.close();
				}
				
				if (isr!=null) {
					isr.close();
				}
				
				if (br!=null) {
					br.close();
				}
			}catch(IOException e) {
			}
		}
	}
}
